package com.example.mahoutdemo.core.customer.commodity.operation;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mahoutdemo.config.FilterRescorer;
import com.example.mahoutdemo.utils.RecommendFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.IDRescorer;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CustomerCommodityOperationService extends ServiceImpl<CustomerCommodityOperationMapper, CustomerCommodityOperation> {


    /**
     * 获取用户喜好，进行视频的推送
     */
    public List<Long> recommend(Long userId) throws TasteException {
        List<CustomerPreference> list = baseMapper.listAllCustomerPreference();
        //创建模型数据
        DataModel dataModel = RecommendFactory.buildJdbcDataModel(list);
        //2.创建similar相似度
        UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
        //3.获取用户userNeighborhood
        UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(5, similarity, dataModel);

        //4.构建推荐器recommend
        Recommender recommender = new GenericUserBasedRecommender(dataModel, userNeighborhood, similarity);

        IDRescorer rescorer = new FilterRescorer(list.stream().map(CustomerPreference::getCustomerId).collect(Collectors.toSet()));
        //展示类似的5个商品
        List<RecommendedItem> recommendedItems = recommender.recommend(userId, 5,rescorer);
        return recommendedItems.stream().map(RecommendedItem::getItemID).collect(Collectors.toList());

    }

    public List<Long> recommendItem(Long itemId) throws TasteException {
        List<CustomerPreference> list = baseMapper.listAllCustomerPreference();
        //创建模型数据
        DataModel dataModel = RecommendFactory.buildJdbcDataModel(list);
        //2.创建similar相似度
        ItemSimilarity similarity = RecommendFactory.itemSimilarity(RecommendFactory.SIMILARITY.PEARSON,dataModel);

        //4.构建推荐器recommend
        Recommender recommender = RecommendFactory
                .itemRecommender(similarity,true)
                .buildRecommender(dataModel);

        //展示类似的5个商品
        List<RecommendedItem> recommendedItems = recommender.recommend(itemId, 5);
        return recommendedItems.stream().map(RecommendedItem::getItemID).collect(Collectors.toList());
    }
}
